package cn.itsource.redis.common.controller;

import cn.itsource.common.RedisClient;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.RedisUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController implements RedisClient {
    /*向缓存存储数据*/
    @PostMapping("/redis")
    public AjaxResult set(@RequestParam("key")String key, @RequestParam("value")String value){
        try {
            RedisUtils.INSTANCE.set(key, value);
            return AjaxResult.me().setSuccess(true).setMessage("存入缓存成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("存入缓存失败"+e.getMessage());
        }
    }

    /*向缓存获取数据*/
    @GetMapping("/redis")
    public AjaxResult get(@RequestParam("key")String key){
        try {
            String s = RedisUtils.INSTANCE.get(key);
            return AjaxResult.me().setSuccess(true).setMessage("查询缓存成功!").setResultObj(s);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询缓存失败"+e.getMessage());
        }
    }
}
